package com.derbysoft.lowcostadapter.web.action.admin.config;

import com.derbysoft.lowcostadapter.commons.model.Channel;
import com.derbysoft.lowcostadapter.commons.model.ProviderIdentification;
import com.derbysoft.lowcostadapter.commons.model.SystemVariable;
import com.derbysoft.lowcostadapter.commons.redis.service.ChannelCacheService;
import com.derbysoft.lowcostadapter.commons.redis.service.ProviderIdentificationCacheService;
import com.derbysoft.lowcostadapter.commons.redis.service.SystemVariableCacheService;
import com.derbysoft.lowcostadapter.web.action.BaseAdapterAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by: jason
 * Date: 2012-09-28
 */
public class LoadFromCacheAction extends BaseAdapterAction {

    private SystemVariable condition;

    @Autowired
    private SystemVariableCacheService systemVariableCacheService;

    @Autowired
    private ProviderIdentificationCacheService providerIdentificationCacheService;

    @Autowired
    private ChannelCacheService channelCacheService;

    @Action("loadFromCache")
    public void loadFromCache() {
        try {
            if (StringUtils.isBlank(condition.getName())) {
                throw new IllegalArgumentException("required field is null !!");
            }
            condition = systemVariableCacheService.loadByName(condition.getName());

            populateSucceededJsonResult(condition != null ? condition.value() : NOT_FOUND);
        } catch (Exception ex) {
            populateFailedJsonResult(ex);
        }
    }

    @Action("loadChannels")
    public void loadChannels() {
        try {
            StringBuilder channelBuilder = new StringBuilder();
            for (Channel channel : channelCacheService.loadAll()) {
                channelBuilder.append(String.format("Channel:[%s], AvailabilityProvider:[%s]", channel.getChannelCode(), channel.getAvailabilityProvider())).append("<br>");
            }
            populateSucceededJsonResult(StringUtils.isNotBlank(channelBuilder.toString()) ? channelBuilder.toString() : NOT_FOUND);
        } catch (Exception ex) {
            populateFailedJsonResult(ex);
        }
    }

    @Action("loadIdentifications")
    public void loadIdentifications() {
        try {
            StringBuilder builder = new StringBuilder();
            for (ProviderIdentification identification : providerIdentificationCacheService.loadAll()) {
                builder.append(String.format("Channel: [%s], UserName: [%s]", identification.getChannelCode(), identification.getUserName())).append("<br>");
            }
            populateSucceededJsonResult(StringUtils.isNotBlank(builder.toString()) ? builder.toString() : NOT_FOUND);
        } catch (Exception ex) {
            populateFailedJsonResult(ex);
        }
    }

    public SystemVariable getCondition() {
        return condition;
    }

    public void setCondition(SystemVariable condition) {
        this.condition = condition;
    }
}
